<?php
/**
 * 站内文章控制器
 */
namespace Admin\Controller;
use Think\Controller;
class ArticleSiteController extends InitController {
    
	// 站内文章列表
	public function articleList() {
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$where = '';
		$title = I('get.title','','strip_tags,htmlspecialchars,trim');
		
		if (!empty($title)) {
			$where['s.title'] = array('LIKE','%' . $title . '%');
			$this->assign('title',$title);
		}
		$start_time = I('get.start_time','','strip_tags,htmlspecialchars,trim');
		$end_time = I('get.end_time','','strip_tags,htmlspecialchars,trim');
	
		if ($start_time !== '') {
			$start_time = strtotime($start_time);
			$this->assign('start_time',$start_time);
		}
		
		if ($end_time !== '') {
			$end_time = strtotime($end_time);
			$this->assign('end_time',$end_time);
		}
		
		if (!empty($start_time) && !empty($end_time)) {
			// 如果有开始及结束时间 则取中值
			$where['s.createtime'] = array(array('EGT', $start_time), array('ELT', $end_time));
		}else{
			if (!empty($start_time)) {
				// 有开始时间
				$where['s.createtime'] = array('EGT', $start_time);
			}elseif (!empty($end_time)) {
				// 有结束时间
				$where['s.createtime'] = array('ELT', $end_time);
			}
		}
		
		$status = I('get.status','','strip_tags,htmlspecialchars,trim');
		if($status !== ''){
			if($status != 'all'){
				$status = $status + 0;
				$where['s.status'] = $status;
			}
		}

		$articleModel = D('ArticleSite');
		if (!empty($where)) {
			$total = $articleModel->where($where)->alias('s')->count();
		} else {
			$total = $articleModel->getArticleNum($where);
		}

		$Page = $this->setPageHander($total);
		$show = $Page?$Page->show():'';
		$list = $articleModel->getArticleList($where,'s.taxis desc,s.id desc',$Page->firstRow.','.$Page->listRows,1);

		$this->assign('status',$status);
		$this->assign('list',$list);
		$this->assign('pager',$show);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Article/articleList');
	}
	
	// 发布文章
	public function articleAdd() {
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		$cate = D('articleCate')->getArticleCateList('', 'id asc');
		$this->assign('cate',$cate);
		$this->assign(array(
			'defSubTitle' => '发布文章',
			'ajaxUrl' => U('Admin/ArticleSite/do_articleAdd'),
			'data' => array('status' => 1),	// 默认显示
		));
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Article/articleEdit');
	}
	
	// 添加站内文章操作
	public function do_articleAdd() {
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$title = I('title','','strip_tags,htmlspecialchars,trim');
		if (empty($title)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'请输入页面标题！','field'=>''));
		}
		$cid = I('cid',0,'intval');
		if (!$cid) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'请选择栏目！','field'=>''));
		}
		$taxis = I('taxis',100,'intval');
		$status = I('status',0,'intval');
		$content = I('content','','trim');
		$seo_key = I('seo_key','','trim');
		$seo_depict = I('seo_depict','','trim');
		
		$data['title'] = $title;
		$data['admin_id'] = session('uid');
		$data['createtime'] = NOW_TIME;
		$data['status'] = $status;
		$data['content'] = $content;
		$data['cat_id'] = $cid;
		$data['thumb'] = '';
		$data['taxis'] = $taxis;
		$data['seo_key'] = $seo_key;
		$data['seo_depict'] = $seo_depict;
		
		if (M('ArticleSite')->add($data)) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}
	
	// 删除单页
	public function articleDel() {
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$id = I('id',0,'intval');
		if (!id) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误！','field'=>''));
		}
		if (M('ArticleSite')->where(array('id'=>$id))->delete()) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}
	
	// 编辑单页
	public function articleEdit() {
		$id = I('id',0,'intval');
		if (!id) {
			$this->error('参数错误');
		}
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$articleModel = D('ArticleSite');
		$data =  $articleModel->getArticleById($id);		
		$cate = D('ArticleCate')->getArticleCateList('', 'id asc');
		
		$this->assign('cate',$cate);
		$this->assign('data',$data);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Article/articleEdit');
	}
	
	// 编辑单页操作
	public function do_articleEdit() {
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$id = I('id',0,'intval');
		if (!$id) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误！','field'=>''));
		}
		$title = I('title','','strip_tags,htmlspecialchars,trim');
		if (empty($title)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'请输入页面标题！','field'=>''));
		}
		$cid = I('cid',0,'intval');
		if (!$cid) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'请选择栏目！','field'=>''));
		}
		$taxis = I('taxis',100,'intval');
		$status = I('status',0,'intval');
		$content = I('content','','trim');
		$seo_key = I('seo_key','','trim');
		$seo_depict = I('seo_depict','','trim');
		
		$data['title'] = $title;
		$data['status'] = $status;
		$data['content'] = $content;
		$data['cat_id'] = $cid;
		$data['thumb'] = '';
		$data['taxis'] = $taxis;
		$data['seo_key'] = $seo_key;
		$data['seo_depict'] = $seo_depict;
		
		
		if (M('ArticleSite')->where(array('id'=>$id))->save($data) !== false) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}

}